<template>
    投资项目名称:<input type="text" v-model="Iname">
    投资总额度:<input type="text" v-model="MinBudget">-<input type="text" v-model="MaxBudget">
    <input type="button" value="查询"  class="btn btn-primary" @click="Show">


<table class="table-bordered" border="1">
    <thead>
<tr>
    <td>项目编号</td>
    <td>项目名称</td>
    <td>项目总金额</td>
    <td>已投资金额</td>
    <td>项目剩余金额</td>
    <td>投资人数</td>
    <td>操作</td>
</tr>
    </thead>
    <tbody>
<tr v-for="i in info" style="background-color: aqua;">
    <td>{{ i.I_Id }}</td>
    <td>{{ i.I_Name }}</td>
    <td>{{ i.I_Budget }}</td>
    <td>{{ i.I_Receive }}</td>
    <td>{{ i.I_Residue }}</td>
    <td>{{ i.I_Number }}</td>
    <td>
        <a href="#" @click="DeleteInfo(i.I_Id)">删除</a>
        <a href="#" @click="EditInfo(i.I_Id)">修改
    </a>
        
    </td>
</tr>
    </tbody>
</table>
<a href="#" @click="PageLoadInfo('F')">首页</a>
<a href="#" @click="PageLoadInfo('P')">上一页</a>
<a href="#" @click="PageLoadInfo('N')">下一页</a>
<a href="#" @click="PageLoadInfo('L')">尾页</a>
一共<span style="color: red;">{{ PageInfo.ReCount }}</span>条数据，当前第<span style="color: red;">{{ PageInfo.PageIndex }}</span>页，共<span style="color: red;">{{ PageInfo.PageCount }}</span>页
<span style="color: red;">{{ PageInfo.PageSize }}</span>条数据

<select @change="ChangeSize" v-model="Csize">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
</select>
<input type="text" style="width: 30;" v-model="TIndex">
<button @click="TakeShowInfo">Go</button>
</template>
<script setup lang="ts">
import axios from 'axios';
import {ref,onMounted}from 'vue'
import { useRouter } from 'vue-router';
const router=useRouter()
const TIndex=ref(1)
const Iname=ref('')
const MinBudget=ref('')
const MaxBudget=ref('')
const PageInfo=ref({
    PageIndex:1,
    PageSize:2,
    ReCount:0,
    PageCount:0
})
const Csize=ref(2)

const info=ref([
    {
        I_Id:'',
        I_Name:'',
        I_Budget:0,
        I_Receive:0,
        I_Residue:0,
        I_Number:0,
    }
])
onMounted(()=>{
    Show()
})
const Show=()=>{
    axios.get('https://localhost:44358/api/Invest/ShowInvestInfo',
        {
            params:{
                Fname:Iname.value,
                MinBudget:MinBudget.value,
                MaxBudget:MaxBudget.value,
                PageIndex:PageInfo.value.PageIndex,
                PageSize:PageInfo.value.PageSize
            }
        }
        
        ).then(res=>{
            info.value=res.data.list
            PageInfo.value.PageCount=res.data.PageCount
            PageInfo.value.ReCount=res.data.ReCount
        }).catch(err=>{
            console.log(err);
        })
}
const PageLoadInfo=(msg:string)=>{
    if(msg=="F"){
        PageInfo.value.PageIndex=1
    }else if(msg=="P"){
        if(PageInfo.value.PageIndex==1){
            alert("已经是第一页")
            return
        }
        PageInfo.value.PageIndex--
    }else if(msg=="N"){
        if(PageInfo.value.PageIndex==PageInfo.value.PageCount){
            alert("已经是最后一页")
            return
        }
        PageInfo.value.PageIndex++
    }else if(msg=="L"){
        PageInfo.value.PageIndex=PageInfo.value.PageCount
    }
    Show()
    }
    //设置页容量
const ChangeSize=()=>{
    //接受所选参数值
    const Size=Csize.value
    PageInfo.value.PageSize=Size
    Show()
}
const TakeShowInfo=()=>{
    if(isNaN(TIndex.value)){
        alert("请输入数字")
        return
    }else{
        if(TIndex.value<1||TIndex.value>PageInfo.value.PageCount){
        alert("输入的页码不正确")
        return
    }
    }
    
    PageInfo.value.PageIndex=TIndex.value
    Show()
}
//完成删除功能
const DeleteInfo=(Iid:string)=>{
if(!confirm("确认删除嘛")){
    return
}

    axios.get('https://localhost:44358/api/Invest/DeleteInfo',
        {
            params:{
                Iid:Iid
            }
        }
        
        ).then(res=>{
            if(res.data>0){
                alert("删除成功")
                Show()
            }else if(res.data==-1){
                alert("项目有人投资，删除失败")
                Show()
            }else{
                alert("删除失败")
                Show()
            }
        }).catch(err=>{
            console.log(err);
        })
}
//跳转
const EditInfo=(Iid:string)=>{
   router.push(
    {
        path:'/EditInvest',
        name:"EditInvest",
        params:{
            Iid:Iid
        }
    }
   )

}

</script>